//
//  DiaryModelCategory.swift
//  MyDiary2025
//
//  Created by 罗万能 on 2025/6/25.
//

import Foundation
import SwiftData
import SwiftUI

typealias CategoryModel = CategoryModelV1

@Model
class CategoryModelV1{
    var id: UUID
    var categoryName: String
    var categoryImage: Data?
    var categoryDescription: String
    var categoryColor: Color
    
    init(id: UUID, categoryName: String, categoryImage: Data? = nil, categoryDescription: String, categoryColor: Color = .primary) {
        self.id = id
        self.categoryName = categoryName
        self.categoryImage = categoryImage
        self.categoryDescription = categoryDescription
        self.categoryColor = categoryColor
    }
    
}

extension CategoryModel{
    @Transient var image: Image {
        if  self.categoryImage != nil{
            if let uiImage = UIImage(data: self.categoryImage!){
                
                return Image(uiImage: uiImage)
                
            }
        }
        return Image(systemName: "shippingbox.circle")
        
    }
    static var debugData: CategoryModel = {
        var imgDatas:[Data] = []
        for i in 1...15 {	
            if let img = UIImage(named: "\(i)"),let data = img.pngData(){
                imgDatas.append(data)
            }
        }
        return CategoryModel(id: UUID(), categoryName: "Debug", categoryImage: imgDatas[0], categoryDescription: "Debug For model category", categoryColor: .red)
    }()
    
    static var debugDatas: [CategoryModel] = {
        var imgDatas:[Data] = []
        for i in 1...15 {
            if let img = UIImage(named: "\(i)"),let data = img.pngData(){
                imgDatas.append(data)
            }
        }
        return [
            CategoryModel(id: UUID(), categoryName: "Debug", categoryImage: imgDatas[Int.random(in: 0..<15)], categoryDescription: "Debug For model category", categoryColor: .red),
            CategoryModel(id: UUID(), categoryName: "CategoryModel1", categoryImage: imgDatas[Int.random(in: 0..<15)], categoryDescription: "Debug For model category1", categoryColor: .green),
            CategoryModel(id: UUID(), categoryName: "CategoryMode2", categoryImage: imgDatas[Int.random(in: 0..<15)], categoryDescription: "Debug For model category2", categoryColor: .blue),
            CategoryModel(id: UUID(), categoryName: "CategoryMode3", categoryImage: imgDatas[Int.random(in: 0..<15)], categoryDescription: "Debug For model category3", categoryColor: .yellow),
            CategoryModel(id: UUID(), categoryName: "CategoryMode4", categoryImage: imgDatas[Int.random(in: 0..<15)], categoryDescription: "Debug For model category4", categoryColor: .brown),
            CategoryModel(id: UUID(), categoryName: "CategoryMode5", categoryImage: imgDatas[Int.random(in: 0..<15)], categoryDescription: "Debug For model category5", categoryColor: .teal),
        ]
    }()
}
